{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NumPy简介：打开高效数据计算的大门\n",
    "\n",
    "## 🎯 本节学习目标\n",
    "\n",
    "学完本节，你将能够：\n",
    "1. 理解NumPy在数据分析中的核心地位和作用\n",
    "2. 掌握NumPy与Python原生列表的本质区别\n",
    "3. 了解NumPy的性能优势及其背后的原理\n",
    "4. 能够正确安装和导入NumPy库\n",
    "5. 认识NumPy在数据科学生态系统中的位置\n",
    "\n",
    "**预计学习时间：** 30-45分钟\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 📚 从一个实际问题开始\n",
    "\n",
    "### 场景：计算学生成绩的统计信息\n",
    "\n",
    "假设你是一名数据分析师，需要处理100万名学生的考试成绩数据，计算每个学生5门课程的总分、平均分、最高分、最低分等统计信息。\n",
    "\n",
    "**思考：** 如果用Python原生列表，你会怎么做？\n",
    "\n",
    "让我们看看两种实现方式的对比："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法1：使用Python原生列表\n",
      "==================================================\n",
      "处理时间: 0.0736秒\n",
      "前5名学生的平均分: [45.4, 36.6, 67.0, 19.8, 48.8]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "# 模拟100万名学生，每人5门课程的成绩\n",
    "num_students = 1000000\n",
    "num_courses = 5\n",
    "\n",
    "# 方法1：使用Python列表\n",
    "print(\"方法1：使用Python原生列表\")\n",
    "print(\"=\"*50)\n",
    "\n",
    "# 生成随机成绩（0-100分）\n",
    "import random\n",
    "random.seed(42)\n",
    "scores_list = [[random.randint(0, 100) for _ in range(num_courses)] \n",
    "               for _ in range(num_students)]\n",
    "\n",
    "# 计算平均分\n",
    "start_time = time.time()\n",
    "avg_scores_list = [sum(student) / len(student) for student in scores_list]\n",
    "list_time = time.time() - start_time\n",
    "\n",
    "print(f\"处理时间: {list_time:.4f}秒\")\n",
    "print(f\"前5名学生的平均分: {avg_scores_list[:5]}\")\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法2：使用NumPy\n",
      "==================================================\n",
      "处理时间: 0.0114秒\n",
      "前5名学生的平均分: [57.6 67.2 46.4 41.2 35. ]\n",
      "\n",
      "性能对比\n",
      "==================================================\n",
      "🚀 NumPy比Python列表快了 6.5 倍！\n",
      "💡 节省时间: 0.0622秒\n"
     ]
    }
   ],
   "source": [
    "# 方法2：使用NumPy\n",
    "import numpy as np\n",
    "\n",
    "print(\"方法2：使用NumPy\")\n",
    "print(\"=\"*50)\n",
    "\n",
    "# 生成随机成绩\n",
    "np.random.seed(42)\n",
    "scores_array = np.random.randint(0, 101, size=(num_students, num_courses))\n",
    "\n",
    "# 计算平均分\n",
    "start_time = time.time()\n",
    "avg_scores_array = scores_array.mean(axis=1)\n",
    "numpy_time = time.time() - start_time\n",
    "\n",
    "print(f\"处理时间: {numpy_time:.4f}秒\")\n",
    "print(f\"前5名学生的平均分: {avg_scores_array[:5]}\")\n",
    "print()\n",
    "\n",
    "# 性能对比\n",
    "print(\"性能对比\")\n",
    "print(\"=\"*50)\n",
    "speedup = list_time / numpy_time\n",
    "print(f\"🚀 NumPy比Python列表快了 {speedup:.1f} 倍！\")\n",
    "print(f\"💡 节省时间: {list_time - numpy_time:.4f}秒\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**结论：** 这就是为什么我们需要NumPy！当处理大规模数值数据时，NumPy能带来数十倍甚至数百倍的性能提升。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🔍 什么是NumPy？\n",
    "\n",
    "**NumPy**（Numerical Python的缩写）是Python科学计算的基础库，它提供了：\n",
    "\n",
    "### 核心组件\n",
    "1. **ndarray**（N-dimensional array，N维数组）\n",
    "   - 一个快速、灵活的大数据集容器\n",
    "   - 支持向量化操作\n",
    "   - 所有元素类型相同\n",
    "\n",
    "2. **丰富的数学函数库**\n",
    "   - 线性代数运算\n",
    "   - 统计函数\n",
    "   - 随机数生成\n",
    "   - 傅里叶变换等\n",
    "\n",
    "3. **高效的内存管理**\n",
    "   - 连续内存块存储\n",
    "   - 底层用C语言实现\n",
    "\n",
    "### 一句话总结\n",
    "> NumPy是Python中处理数值数据的首选工具，它让大规模数值计算变得简单而高效。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 💡 为什么要学习NumPy？\n",
    "\n",
    "### 1️⃣ 计算效率高\n",
    "- NumPy底层用C语言实现，速度比纯Python快10-100倍\n",
    "- 支持向量化操作，避免了慢速的Python循环\n",
    "\n",
    "### 2️⃣ 内存效率高\n",
    "- NumPy数组占用内存更少\n",
    "- 连续内存块存储，访问速度更快\n",
    "\n",
    "### 3️⃣ 代码简洁易读\n",
    "```python\n",
    "# Python列表方式\n",
    "result = [x**2 + 2*x + 1 for x in data]\n",
    "\n",
    "# NumPy方式（向量化）\n",
    "result = data**2 + 2*data + 1\n",
    "```\n",
    "\n",
    "### 4️⃣ 数据科学的基础\n",
    "- Pandas、Scikit-learn、TensorFlow等库都基于NumPy\n",
    "- 掌握NumPy是学习数据科学的必经之路\n",
    "\n",
    "### 5️⃣ 功能丰富\n",
    "- 数组操作：切片、索引、变形\n",
    "- 数学运算：线性代数、统计、傅里叶变换\n",
    "- 随机数生成：各种概率分布"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 📊 NumPy vs Python列表：深度对比\n",
    "\n",
    "| 对比维度 | Python列表 | NumPy数组 | 说明 |\n",
    "|---------|------------|-----------|------|\n",
    "| **数据类型** | 可以混合不同类型 | 所有元素必须是同一类型 | NumPy的限制换来了性能 |\n",
    "| **内存占用** | 高（每个元素是独立对象） | 低（连续内存块） | NumPy数组更紧凑 |\n",
    "| **计算速度** | 慢（Python解释执行） | 快（C语言实现） | NumPy快10-100倍 |\n",
    "| **数组大小** | 可变（可append、extend） | 固定（创建后大小不变） | NumPy需要创建新数组 |\n",
    "| **多维支持** | 需要嵌套列表 | 原生支持多维 | NumPy更直观 |\n",
    "| **向量化运算** | 不支持（需要循环） | 支持（直接操作整个数组） | NumPy代码更简洁 |\n",
    "| **数学函数** | 基本操作 | 丰富的数学函数库 | NumPy功能强大 |\n",
    "| **适用场景** | 通用数据存储 | 数值计算、科学计算 | 各有所长 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🎬 更多性能对比示例\n",
    "\n",
    "让我们看看在不同场景下NumPy的性能表现："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "场景1：计算100万个数的总和\n",
      "============================================================\n",
      "Python列表: 0.005278秒\n",
      "NumPy数组:  0.000268秒\n",
      "加速比: 19.7倍\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "\n",
    "size = 1000000\n",
    "\n",
    "# 场景1：数组求和\n",
    "print(\"场景1：计算100万个数的总和\")\n",
    "print(\"=\"*60)\n",
    "\n",
    "py_list = list(range(size))\n",
    "np_array = np.arange(size)\n",
    "\n",
    "# Python列表\n",
    "start = time.time()\n",
    "py_sum = sum(py_list)\n",
    "py_time = time.time() - start\n",
    "\n",
    "# NumPy数组\n",
    "start = time.time()\n",
    "np_sum = np_array.sum()\n",
    "np_time = time.time() - start\n",
    "\n",
    "print(f\"Python列表: {py_time:.6f}秒\")\n",
    "print(f\"NumPy数组:  {np_time:.6f}秒\")\n",
    "print(f\"加速比: {py_time/np_time:.1f}倍\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "场景2：计算100万个数的平方\n",
      "============================================================\n",
      "Python列表: 0.045462秒\n",
      "NumPy数组:  0.000882秒\n",
      "加速比: 51.5倍\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 场景2：数学运算（平方）\n",
    "print(\"场景2：计算100万个数的平方\")\n",
    "print(\"=\"*60)\n",
    "\n",
    "# Python列表\n",
    "start = time.time()\n",
    "py_squared = [x**2 for x in py_list]\n",
    "py_time = time.time() - start\n",
    "\n",
    "# NumPy数组\n",
    "start = time.time()\n",
    "np_squared = np_array**2\n",
    "np_time = time.time() - start\n",
    "\n",
    "print(f\"Python列表: {py_time:.6f}秒\")\n",
    "print(f\"NumPy数组:  {np_time:.6f}秒\")\n",
    "print(f\"加速比: {py_time/np_time:.1f}倍\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "场景3：筛选大于50万的数\n",
      "============================================================\n",
      "Python列表: 0.030225秒\n",
      "NumPy数组:  0.001006秒\n",
      "加速比: 30.0倍\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 场景3：条件筛选\n",
    "print(\"场景3：筛选大于50万的数\")\n",
    "print(\"=\"*60)\n",
    "\n",
    "# Python列表\n",
    "start = time.time()\n",
    "py_filtered = [x for x in py_list if x > 500000]\n",
    "py_time = time.time() - start\n",
    "\n",
    "# NumPy数组\n",
    "start = time.time()\n",
    "np_filtered = np_array[np_array > 500000]\n",
    "np_time = time.time() - start\n",
    "\n",
    "print(f\"Python列表: {py_time:.6f}秒\")\n",
    "print(f\"NumPy数组:  {np_time:.6f}秒\")\n",
    "print(f\"加速比: {py_time/np_time:.1f}倍\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 性能总结\n",
    "\n",
    "从上面的测试可以看出：\n",
    "- ✅ NumPy在所有场景下都明显快于Python列表\n",
    "- ✅ 数据量越大，NumPy的优势越明显\n",
    "- ✅ 数学运算操作中，NumPy的优势最为突出\n",
    "\n",
    "**关键原因：**\n",
    "1. NumPy底层用C实现，避免了Python解释器开销\n",
    "2. 向量化操作避免了慢速的Python循环\n",
    "3. 连续内存存储，缓存友好，访问速度快"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🛠️ 安装NumPy\n",
    "\n",
    "NumPy可以通过多种方式安装：\n",
    "\n",
    "### 方法1：使用pip（推荐）\n",
    "```bash\n",
    "pip install numpy\n",
    "```\n",
    "\n",
    "### 方法2：使用Anaconda\n",
    "如果你使用Anaconda发行版，NumPy已经预装好了。如果需要更新：\n",
    "```bash\n",
    "conda install numpy\n",
    "# 或\n",
    "conda update numpy\n",
    "```\n",
    "\n",
    "### 方法3：使用国内镜像（加速下载）\n",
    "```bash\n",
    "pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "```\n",
    "\n",
    "### 验证安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "✅ NumPy安装成功！\n",
      "版本: 2.1.3\n",
      "测试数组: [1 2 3 4 5]\n",
      "数组求和: 15\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 检查NumPy版本\n",
    "print(\"✅ NumPy安装成功！\")\n",
    "print(f\"版本: {np.__version__}\")\n",
    "\n",
    "# 测试基本功能\n",
    "test_array = np.array([1, 2, 3, 4, 5])\n",
    "print(f\"测试数组: {test_array}\")\n",
    "print(f\"数组求和: {test_array.sum()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 📦 导入NumPy\n",
    "\n",
    "在Python中使用NumPy的**标准方式**是："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 约定俗成使用 'np' 作为NumPy的别名\n",
    "# 这样做的好处：\n",
    "# 1. 减少代码量（np 比 numpy 短）\n",
    "# 2. 代码更规范（大家都这样用）\n",
    "# 3. 代码更易读（一看到np就知道是NumPy）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**⚠️ 注意事项：**\n",
    "\n",
    "```python\n",
    "# ❌ 不推荐：导入所有函数到全局命名空间\n",
    "from numpy import *\n",
    "# 问题：可能与其他库的函数名冲突\n",
    "\n",
    "# ✅ 推荐：使用别名\n",
    "import numpy as np\n",
    "\n",
    "# ✅ 也可以：只导入需要的函数\n",
    "from numpy import array, arange, zeros\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🌟 NumPy的实际应用场景\n",
    "\n",
    "NumPy不仅仅是一个计算工具，它在很多实际场景中都有广泛应用：\n",
    "\n",
    "### 1. 数据分析\n",
    "```python\n",
    "# 计算股票收益率\n",
    "prices = np.array([100, 102, 98, 105, 103])\n",
    "returns = (prices[1:] - prices[:-1]) / prices[:-1]\n",
    "print(f\"日收益率: {returns}\")\n",
    "```\n",
    "\n",
    "### 2. 图像处理\n",
    "```python\n",
    "# 图像实际上就是NumPy数组\n",
    "# 例如：一个RGB图像是 (高度, 宽度, 3) 的三维数组\n",
    "image = np.random.randint(0, 255, size=(100, 100, 3))\n",
    "```\n",
    "\n",
    "### 3. 机器学习\n",
    "```python\n",
    "# 标准化特征（常用的预处理方法）\n",
    "data = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "mean = data.mean(axis=0)\n",
    "std = data.std(axis=0)\n",
    "normalized = (data - mean) / std\n",
    "```\n",
    "\n",
    "### 4. 科学计算\n",
    "```python\n",
    "# 生成正弦波\n",
    "x = np.linspace(0, 2*np.pi, 100)\n",
    "y = np.sin(x)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🌐 NumPy在Python数据科学生态中的位置\n",
    "\n",
    "NumPy是Python科学计算生态系统的**基石**：\n",
    "\n",
    "```\n",
    "                    应用层\n",
    "        ┌─────────────────────────────┐\n",
    "        │  机器学习、深度学习、数据可视化  │\n",
    "        └─────────────────────────────┘\n",
    "                      ↑\n",
    "                    工具层\n",
    "    ┌──────────┬──────────┬──────────┬──────────┐\n",
    "    │ Pandas   │ Matplotlib│ Scikit   │ TensorFlow│\n",
    "    │(数据分析) │ (可视化)  │ -learn   │ PyTorch   │\n",
    "    │          │          │(机器学习) │(深度学习)  │\n",
    "    └──────────┴──────────┴──────────┴──────────┘\n",
    "                      ↑\n",
    "                    基础层\n",
    "            ┌──────────────────┐\n",
    "            │      NumPy       │\n",
    "            │  (数值计算基础)   │\n",
    "            └──────────────────┘\n",
    "                      ↑\n",
    "                  底层语言\n",
    "            ┌──────────────────┐\n",
    "            │   C / Fortran    │\n",
    "            └──────────────────┘\n",
    "```\n",
    "\n",
    "### 主要依赖NumPy的库：\n",
    "\n",
    "- **SciPy**：科学计算库，提供优化、积分、插值等高级算法\n",
    "- **Pandas**：数据分析库，DataFrame底层就是NumPy数组\n",
    "- **Matplotlib**：数据可视化库，绘图数据使用NumPy数组\n",
    "- **Scikit-learn**：机器学习库，所有算法都基于NumPy实现\n",
    "- **TensorFlow/PyTorch**：深度学习库，与NumPy无缝集成\n",
    "\n",
    "### 为什么NumPy这么重要？\n",
    "\n",
    "1. **统一的数据接口**：所有库都使用NumPy数组作为数据交换格式\n",
    "2. **高性能基础**：为上层库提供高效的数值计算能力\n",
    "3. **生态系统核心**：学会NumPy，就能轻松使用其他数据科学库"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 🎓 小结\n",
    "\n",
    "在这一节中，我们学习了：\n",
    "\n",
    "### ✅ 核心知识点\n",
    "1. **NumPy是什么**：Python科学计算的基础库，核心是ndarray\n",
    "2. **为什么用NumPy**：速度快、内存省、代码简洁、功能强大\n",
    "3. **NumPy vs 列表**：NumPy在数值计算上有10-100倍的性能优势\n",
    "4. **如何安装使用**：`pip install numpy` 和 `import numpy as np`\n",
    "5. **NumPy的地位**：Python数据科学生态的基石\n",
    "\n",
    "### 💭 关键要点\n",
    "- NumPy数组所有元素类型必须相同（这是性能的代价）\n",
    "- 向量化操作是NumPy的核心优势\n",
    "- 掌握NumPy是学习Pandas、机器学习等的基础\n",
    "\n",
    "### 🎯 下一步学习\n",
    "在下一节中，我们将深入学习：\n",
    "- NumPy数组的创建方法\n",
    "- 数组的属性和基本操作\n",
    "- 数组的索引和切片\n",
    "- 数组的形状操作\n",
    "\n",
    "---\n",
    "\n",
    "**准备好了吗？让我们继续探索NumPy的强大功能！** 🚀"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 📚 附录：NumPy与MATLAB的对比\n",
    "\n",
    "*（供有MATLAB经验的学习者参考）*\n",
    "\n",
    "| 特性 | MATLAB | NumPy |\n",
    "|------|--------|-------|\n",
    "| 索引起始 | 从1开始 | 从0开始 |\n",
    "| 矩阵乘法 | `A * B` | `A @ B` 或 `np.dot(A, B)` |\n",
    "| 元素乘法 | `A .* B` | `A * B` |\n",
    "| 数组大小 | `size(A)` | `A.shape` |\n",
    "| 数组切片 | 复制数据 | 引用数据（视图） |\n",
    "| 许可证 | 商业软件（付费） | 开源免费 |\n",
    "\n",
    "**NumPy的优势：**\n",
    "- 免费开源\n",
    "- 与Python生态无缝集成\n",
    "- 社区活跃，库丰富\n",
    "- 可以轻松部署到服务器\n",
    "\n",
    "---\n",
    "\n",
    "## 🤔 常见问题 FAQ\n",
    "\n",
    "**Q1: NumPy数组和Python列表，应该用哪个？**\n",
    "- A: 如果是数值计算，用NumPy；如果是通用数据存储（混合类型），用列表\n",
    "\n",
    "**Q2: NumPy数组的大小可以改变吗？**\n",
    "- A: 不能直接改变，需要创建新数组。但可以用 `np.append()`, `np.concatenate()` 等函数\n",
    "\n",
    "**Q3: 为什么NumPy这么快？**\n",
    "- A: 三个原因：① 底层C实现 ② 向量化避免循环 ③ 连续内存存储\n",
    "\n",
    "**Q4: 学习NumPy需要什么基础？**\n",
    "- A: 基本的Python语法即可，不需要额外的数学知识\n",
    "\n",
    "**Q5: NumPy能处理多大的数据？**\n",
    "- A: 取决于你的内存大小。NumPy数组存在内存中，数据量不能超过可用内存"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
